package com.example.yls.demob;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private LocalReceiver receiver;
    //只能动态注册本地广播
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        receiver = new LocalReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("LOCAL_BROADCAST");
        LocalBroadcastManager manager=LocalBroadcastManager
                .getInstance(MainActivity.this);
        manager.registerReceiver(receiver,filter);

        findViewById(R.id.btn_local_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("LOCAL_BROADCAST");
                /*在本应用*/
                LocalBroadcastManager manager=LocalBroadcastManager
                        .getInstance(MainActivity.this);
                manager.sendBroadcast(intent);

                //跨应用
               /* sendOrderedBroadcast(intent,null);*/
            }
        });
    }


}
